package com.xuxin.nantai.activity.hang;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.nantai.R;
import com.xuxin.nantai.activity.quan.DefectActivity;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.bean.AllAreaBean;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.HangUpdateLoginBean;
import com.xuxin.nantai.bean.ProductInfoBean;
import com.xuxin.nantai.contract.AllAreaContract;
import com.xuxin.nantai.contract.HangUpdateContract;
import com.xuxin.nantai.presenter.AllAreaPresenter;
import com.xuxin.nantai.presenter.HangUpdatePresenter;
import com.xuxin.nantai.utils.ACache;
import com.xuxin.nantai.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.18 0018
 * 绗缝巡检新增修改
 */
public class HangUpdateActivity extends BaseActivity implements HangUpdateContract.View, AllAreaContract.View {
    private HangUpdateContract.Presenter presenter = new HangUpdatePresenter(this);
    private AllAreaContract.Presenter presenter2 = new AllAreaPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.hang_update_name)
    EditText hang_update_name;//成品信息
    @BindView(R.id.hang_update_area)
    TextView hang_update_area;//厂区
    @BindView(R.id.hang_update_pack)
    EditText hang_update_pack;//抽检数
    @BindView(R.id.hang_update_defect)
    TextView hang_update_defect;//疵点数量
    @BindView(R.id.hang_update_inspector)
    TextView hang_update_inspector;//检验员
    @BindView(R.id.hang_update_time)
    TextView hang_update_time;//检验日期
    @BindView(R.id.hang_update_desc)
    TextView hang_update_desc;//疵点描述
    @BindView(R.id.hang_update_remark)
    EditText hang_update_remark;//备注

    private String Guid;//唯一Guid(新增时为空)
    private String ID;//唯一ID(新增时为空)
    private String cInvName;//成品名称
    private String PackQty;//抽检数
    private String InspectorCode;//检验员代码(当前登录人代码)
    private String InspectorName;//检验员姓名(当前登录人姓名)
    private String InspectTime;//检验日期
    private String FacArea;//厂区
    private String DefectNum;//疵点数量
    private String DefectDes;//疵点描述
    private String Remark;//备注

    private MaterialDialog materialDialog;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_hang_update);
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle && !StringUtils.isEmpty(bundle.getString("Guid"))) {
            title_name.setText("绗缝巡检修改");
            Guid = bundle.getString("Guid");
            ID = bundle.getString("ID");
            cInvName = bundle.getString("cInvName");
            PackQty = bundle.getString("PackQty");
            InspectorName = bundle.getString("InspectorName");
            InspectorCode = bundle.getString("InspectorCode");
            InspectTime = bundle.getString("InspectTime");
            FacArea = bundle.getString("FacArea");
            DefectNum = bundle.getString("DefectNum");
            DefectDes = bundle.getString("DefectDes");
            Remark = bundle.getString("Remark");

            hang_update_name.setText(cInvName);
            hang_update_area.setText(FacArea);
            hang_update_pack.setText(PackQty);
            hang_update_defect.setText(DefectNum);
            hang_update_inspector.setText(InspectorName);
            hang_update_time.setText(InspectTime);
            hang_update_desc.setText(DefectDes);
            hang_update_remark.setText(Remark);
        } else {
            title_name.setText("绗缝巡检新增");
            InspectorCode = ACache.get(this).getAsString("LoginName");
            InspectorName = ACache.get(this).getAsString("UserName");
            hang_update_inspector.setText(InspectorName);
        }
        //新增
        if (StringUtils.isEmpty(Guid)) {
            //默认日期今天
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            Date date = new Date(System.currentTimeMillis());
            InspectTime = simpleDateFormat.format(date);
            hang_update_time.setText(InspectTime);
        }
    }

    @Override
    public void initData() {
        if (StringUtils.isEmpty(Guid)) {
            handler.sendEmptyMessage(0);//获取默认厂
        }
        handler.sendEmptyMessage(1);//获取成品名称
        handler.sendEmptyMessage(2);//获取厂区列表
    }

    @Override
    public void onComplete() {
        if (null != materialDialog) {
            materialDialog.dismiss();
        }
    }

    @Override
    @OnClick({R.id.title_left, R.id.hang_update_name_search, R.id.hang_update_time,
            R.id.hang_update_gain, R.id.hang_update_update, R.id.hang_update_area})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.hang_update_name_search:
                if (null != productInfoBean && null != productInfoBean.getData()) {
                    List<String> array = calculate();
                    if (null != array && array.size() > 0) {
                        new MaterialDialog.Builder(this)
                                .title("请选择成名名称")
                                .items(array)
                                .itemsCallback((dialog, view1, position, text) -> {
                                    cInvName = array.get(position).trim();
                                    hang_update_name.setText(cInvName);
                                })
                                .show();
                    } else {
                        ToastUtils.showShort("暂无此成品信息");
                    }
                } else {
                    ToastUtils.showShort("数据加载失败");
                }
                break;
            case R.id.hang_update_time:
                TimeUtils.showDate(this, time -> {
                    InspectTime = time;
                    hang_update_time.setText(InspectTime);
                });
                break;
            case R.id.hang_update_gain:
                try {
                    Bundle bundle = new Bundle();
                    bundle.putString("Guid", Guid);
                    bundle.putString("InsType", "绗缝");
                    ActivityUtils.startActivityForResult(bundle, this,
                            DefectActivity.class, BaseConstant.result_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.hang_update_update:
                try {
                    cInvName = hang_update_name.getText().toString();
                    FacArea = hang_update_area.getText().toString();
                    PackQty = hang_update_pack.getText().toString();
                    InspectorName = hang_update_inspector.getText().toString();
                    InspectTime = hang_update_time.getText().toString();
                    DefectNum = hang_update_defect.getText().toString();
                    DefectDes = hang_update_desc.getText().toString();
                    Remark = hang_update_remark.getText().toString();
                    if (StringUtils.isEmpty(cInvName)) {
                        ToastUtils.showShort("请选择成品名称");
                        return;
                    }
                    if (StringUtils.isEmpty(FacArea)) {
                        ToastUtils.showShort("请输入厂区");
                        return;
                    }
                    if (StringUtils.isEmpty(PackQty)) {
                        ToastUtils.showShort("请输入抽检数");
                        return;
                    }
                    if (StringUtils.isEmpty(InspectorName)) {
                        ToastUtils.showShort("请选择检验员");
                        return;
                    }
                    if (StringUtils.isEmpty(InspectTime)) {
                        ToastUtils.showShort("请选择检验日期");
                        return;
                    }
                    materialDialog = new MaterialDialog.Builder(this)
                            .content("提交数据中...")
                            .progress(true, 0)
                            .cancelable(false)
                            .show();
                    handler.sendEmptyMessage(5);//保存数据
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.hang_update_area:
                if (null != allAreaBean && null != allAreaBean.getData() && allAreaBean.getData().size() > 0) {
                    new MaterialDialog.Builder(this)
                            .items(allAreaBean.getData())
                            .itemsCallback((dialog, view1, position, text) -> {
                                hang_update_area.setText(allAreaBean.getData().get(position).getItemName());
                            })
                            .show();
                }
                break;
        }
    }

    private Handler handler = new Handler(message -> {
        String LoginName = ACache.get(this).getAsString("LoginName");
        String UserName = ACache.get(this).getAsString("UserName");
        LogUtils.e("message.what = " + message.what);
        switch (message.what) {
            case 0://获取默认厂
                presenter2.getDefaultArea(LoginName);
                break;
            case 1://获取成品名称
                presenter.getAllProductInfo("绗缝");
                break;
            case 2://获取厂区列表
                presenter2.getAllArea();
                break;
            case 5://保存数据
                LogUtils.e("Guid = " + Guid);
                LogUtils.e("ID = " + ID);
                LogUtils.e("cInvName = " + cInvName);
                LogUtils.e("PackQty = " + PackQty);
                LogUtils.e("InspectorName = " + InspectorName);
                LogUtils.e("InspectorCode = " + InspectorCode);
                LogUtils.e("InspectTime = " + InspectTime);
                LogUtils.e("FacArea = " + FacArea);
                LogUtils.e("DefectNum = " + DefectNum);
                LogUtils.e("DefectNum = " + DefectNum);
                LogUtils.e("DefectDes " + DefectDes);
                presenter.update(LoginName,
                        UserName,
                        Guid,
                        ID,
                        cInvName,
                        PackQty,
                        InspectorName,
                        InspectorCode,
                        InspectTime,
                        FacArea,
                        DefectNum,
                        DefectDes,
                        Remark);
                break;
        }
        return false;
    });

    @Override
    public void onSuccess(HangUpdateLoginBean hangUpdateLoginBean) {
        try {
            FacArea = hangUpdateLoginBean.getData().get(0).getStaffArea();
            hang_update_area.setText(FacArea);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ProductInfoBean productInfoBean;

    @Override
    public void onSuccess(ProductInfoBean productInfoBean) {
        this.productInfoBean = productInfoBean;
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort(baseBean.getMessage());
        setResult(BaseConstant.result_code);
        finish();
    }

    private AllAreaBean allAreaBean;

    @Override
    public void onSuccess(AllAreaBean allAreaBean) {
        this.allAreaBean = allAreaBean;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (BaseConstant.result_code == resultCode && null != data) {
            DefectDes = data.getStringExtra("DefectDes");
            DefectNum = data.getStringExtra("DefectNum");
            hang_update_desc.setText(DefectDes);
            hang_update_defect.setText(DefectNum);
        }
    }

    /**
     * 计算成名名称显示
     */
    public List<String> calculate() {
        List<String> arrayList = new ArrayList<>();//显示数据
        String msg = hang_update_name.getText().toString().trim();
        for (ProductInfoBean.DataBean item : productInfoBean.getData()) {
            if (item.getCInvName().trim().contains(msg.trim())) {
                arrayList.add(item.getCInvName().trim());
            }
        }
        return arrayList;
    }
}